﻿/// <reference path="../Script/XrmServiceToolkit.js" />
/// <reference path="../Script/date.js" />
/// <reference path="../Script/jquery.js" />

var CRMUsage = CRMUsage || {};

CRMUsage.UsageManager = {
    RetriveAuditUsage: function (successCallback) {
        var options = "orderby=CreatedOn desc&$filter=Operation/Value eq 1 or Operation/Value eq 2";

        var items = [], key, userObject;
        XrmServiceToolkit.Rest.RetrieveMultiple("AuditSet", options,
            //Success (called once for every page of data)
            function (data) {
                //Insert data into correct day, and add the day in a sorted manner
                jQuery.each(data, function (index, val) {
                    //create user if this is the first audit for that user
                    if (!items[val.UserId.Id]) {
                        var tempUserObj = { name: val.UserId.Name, data: [], dictionary: {} };
                        items[val.UserId.Id] = tempUserObj;
                        items.push(tempUserObj);
                    }
                    userObject = items[val.UserId.Id];

                    //create a date for this user  if thie is the first audit for that user for that day
                    var dateArray = CRMUsage.UsageManager.getDateArrayFromUserObject(userObject, val.CreatedOn);

                    //add 1 for this entry
                    dateArray.countArray[1]++;
                });


            },
            //error
            function (data) {

            },
            //completed
            function () {
                //Create days for inactivity with the value of 0
                CRMUsage.UsageManager.createDaysWithZeroValue(items);

                //Sort every data list for each user
                jQuery.each(items, function (index, userObject) {
                    userObject.data.sort(CRMUsage.UsageManager.compare);
                });

                successCallback(items);
            }, true);
    },

    createDaysWithZeroValue: function (listOfUsers) {
        //find first and last days
        var firstDay = new Date(8640000000000000); // date max value
        var lastDay = new Date(-8640000000000000); // date min value
        jQuery.each(listOfUsers, function (index, userObject) {
            jQuery.each(userObject.dictionary, function (jndex, dateAndCount) {
                var dateonly = dateAndCount.date;
                //var dateUTC = Date.UTC(val.CreatedOn.getFullYear(), val.CreatedOn.getMonth(), val.CreatedOn.getDate());
                if (dateonly < firstDay)
                    firstDay = dateonly;
                if (lastDay < dateonly)
                    lastDay = dateonly;
            });
        });

        //add dates with 0 if they don't exist
        jQuery.each(listOfUsers, function (index, userObject) {
            var currentDay = new Date(firstDay);
            while (currentDay < lastDay) {
                CRMUsage.UsageManager.getDateArrayFromUserObject(userObject, currentDay);
                currentDay = currentDay.addDays(1);
            }
        });
    },

    // returns the inner array ( [Date.UTC(1970, 9, 27), 0 ] ) from
    //data: [
    //    [Date.UTC(1970, 9, 27), 0 ],
    //    [Date.UTC(1970, 10, 10), 0.6 ],
    //    [Date.UTC(1970, 10, 18), 0.7 ],
    //    [Date.UTC(1971, 5, 12), 0 ]
    //    ]
    getDateArrayFromUserObject: function (userObject, date) {
        var dateOnly = date.clearTime();
        var dateUTC = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate());
        if (!userObject.dictionary[dateOnly]) {
            var tempDateArray = [dateUTC, 0];
            userObject.dictionary[dateOnly] = { date: dateOnly, countArray: tempDateArray };
            userObject.data.push(tempDateArray);
        }
        return userObject.dictionary[dateOnly];
    },

    compare: function (a, b) {
        if (a[0] < b[0])
            return -1;
        if (a[0] > b[0])
            return 1;
        return 0;
    }
};
